package org.example.novel.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.validation.constraints.NotNull;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${win.file.url}")
    private String winUrl;
    @Value("${win.file.path}")
    private String winPath;

    @Value("${lin.file.url}")
    private String url;
    @Value("${lin.file.path}")
    private String path;

    @Value("jwt.token.key")
    private String key;

    public static String KEY;

    public static String staticRul;

    public static String staticPath;

    @Bean
    public void set(){
        if (verifyOs()){
            setStaticRul(this.winUrl);
            setStaticPath(this.winPath);
        }else{
            setStaticRul(this.url);
            setStaticPath(this.path);
        }
        setKEY(key);
    }

    @Override
    public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
        if (verifyOs()){
            registry.addResourceHandler(winUrl+"**").addResourceLocations("file:"+winPath);
        }else{
            registry.addResourceHandler(url+"**").addResourceLocations("file:"+path);
        }

    }

    public static String getStaticRul() {
        return staticRul;
    }

    public static void setStaticRul(String staticRul) {
        WebMvcConfig.staticRul = staticRul;
    }

    public static String getStaticPath() {
        return staticPath;
    }

    public static void setStaticPath(String staticPath) {
        WebMvcConfig.staticPath = staticPath;
    }

    public static String getKEY() {
        return KEY;
    }

    public static void setKEY(String KEY) {
        WebMvcConfig.KEY = KEY;
    }

    private boolean verifyOs(){
        String property = System.getProperty("os.name");
        return property.toLowerCase().startsWith("win");
    }




}
